package com.yun.algorithm.divideandconquer.leetcode;

public class QuickPowLeetcode50 {

    static double myPow(double x, int n) {
        int p = n;
        if (p < 0) {
            p = -p;
        }
        double r = myPowPositive(x, p);
        return n < 0 ? 1 / r : r;
    }

    static double myPowPositive(double x, int n) {
        if (n == 0) {
            return 1.0;
        }
        if (n == 1) {
            return x;
        }
        double y = myPowPositive(x, n >> 1);
        if ((n & 1) == 0) {
            return y * y;
        } else {
            return x * y * y;
        }
    }

    public static void main(String[] args) {
        System.out.println(myPow(2, 16));
    }
}
